package com.zsp.comic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zsp.comic.entity.Friend;
import com.zsp.comic.entity.JsonResult;
import com.zsp.comic.entity.ResultCode;
import com.zsp.comic.service.FriendService;
import com.zsp.comic.util.ResultTool;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gan
 * @create 2021-01-16 20:12
 */
@RestController
@RequestMapping("/friend")
public class FriendController {
    @Resource
    private FriendService friendService;

    //判断所查单个数据是否为空
    private JsonResult hasOneData(Friend friend){
        if (friend == null){
            return ResultTool.fail(ResultCode.COMMON_FAIL);
        }else {
            return ResultTool.success(friend);
        }
    }

    //判断所查多个数据的集合是否为空
    private JsonResult hasManyData(List<Friend> friends){
        if (friends == null){
            return ResultTool.fail(ResultCode.COMMON_FAIL);
        }else {
            return ResultTool.success(friends);
        }
    }

    //判断所查多个数据的分页对象是否为空
    private JsonResult hasManyData(PageInfo<Friend> pageInfo){
        if (pageInfo == null){
            return ResultTool.fail(ResultCode.COMMON_FAIL);
        }else {
            return ResultTool.success(pageInfo);
        }
    }


    @GetMapping("/getAllFriend/{page}/{size}")
    public JsonResult getAllFriend(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageHelper.startPage(page,size);
        List<Friend> friends = friendService.getAllFriend();
        PageInfo<Friend> pageInfo = new PageInfo<>(friends);
        return hasManyData(pageInfo);
    }

    @GetMapping("/getFriendById/{id}")
    public JsonResult getFriendById(@PathVariable Integer id){
        Friend friend = friendService.getFriendById(id);
        return hasOneData(friend);
    }

    @PostMapping("/getFriendByName/{page}/{size}")
    public JsonResult getFriendByName(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @RequestBody Friend friend){
        PageHelper.startPage(page,size);
        List<Friend> friends = friendService.getFriendByName(friend.getName());
        PageInfo<Friend> pageInfo = new PageInfo<>(friends);
        return hasManyData(pageInfo);
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/test")
    public JsonResult test(){
        System.out.println("test执行了");
        return ResultTool.success("test执行了");
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/getAllFriendWithAdminAuthority/{page}/{size}")
    public JsonResult getAllFriendWithAdminAuthority(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageHelper.startPage(page,size);
        List<Friend> friends = friendService.getAllFriendWithAdminAuthority();
        PageInfo<Friend> pageInfo = new PageInfo<>(friends);
        return hasManyData(pageInfo);
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/getFriendByIdWithAdminAuthority/{id}")
    public JsonResult getFriendByIdWithAdminAuthority(@PathVariable Integer id){
        Friend friend = friendService.getFriendByIdWithAdminAuthority(id);
        return hasOneData(friend);
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/getFriendByNameWithAdminAuthority/{page}/{size}")
    public JsonResult getFriendByNameWithAdminAuthority(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @RequestBody Friend friend){
        PageHelper.startPage(page,size);
        List<Friend> friends = friendService.getFriendByNameWithAdminAuthority(friend.getName());
        PageInfo<Friend> pageInfo = new PageInfo<>(friends);
        return hasManyData(pageInfo);
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/addFriend")
    public JsonResult addFriend(@RequestBody Friend friend){
        if (null != friend){
            return ResultTool.success(friendService.addFriend(friend));
        }
        return ResultTool.fail();
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/updateFriend")
    public JsonResult updateFriend(@RequestBody Friend friend){
        if (null != friend){
            return ResultTool.success(friendService.updateFriend(friend));
        }
        return ResultTool.fail();
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/deleteFriend")
    public JsonResult deleteFriend(@RequestParam Integer friendId){
//        System.out.println(friendId);
        return ResultTool.success(friendService.deleteFriend(friendId));
    }

}
